<?php

/**
 * uniBoard User Profile
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-20 21:23:27 +0000 (Mon, 20 Sep 2010) $
 * $Revision: 64 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_user{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * User API
	 *
	 * @var object
	 */
	
	private $api 			= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Load Language File
		$appCore -> loadLanguageFile('users');
		
		// Can we see users list?
		if ( !$appCore -> user_acl['can_see_users_profiles'] && $appCore -> user['user_id'] != $appCore -> _REQ('user'))
		{
			// Set Error
			$appCore -> output -> setEvent( $appCore -> lang['action_not_allowed_access'], $appCore -> lang['users_list'], 0);
			
			// Draw Events
			$appCore -> output -> drawEvents();
		}
		else if ( $appCore -> _REQ('user') < 1)
		{
			// Set Error
			$appCore -> output -> setEvent( $appCore -> lang['api_user_not_found'], $appCore -> lang['board_nav_users'], 0);
			
			// Run Page
			$appCore -> output -> drawEvents();
		}
		else
		{
			// Start API
			$this -> api = new api_user($appCore, $appCore -> _REQ('user'));
			
			// User not found
			if ( $this -> api -> error)
			{
				// Set Error
				$appCore -> output -> setEvent( $appCore -> lang['api_user_not_found'], $appCore -> lang['board_nav_users'], 0);
				
				// Run Page
				$appCore -> output -> drawEvents();
			}
			else
			{
				// Load Templates
				$templates = new tpl_user_profile( $appCore);
								
				// Main template vars
				$tpl_vars = array(
					'user_id' 				=> $this -> api -> user_id,
					'user_name'				=> $this -> api -> user_name,
					'user_title'			=> $this -> api -> user_title,
					'user_email'			=> htmlspecialchars($this -> api -> user_email),
					'user_want_emails'		=> ($appCore -> user_acl['email_access'] == 0 ? 0 : $appCore -> user_acl['email_access'] == 2 || $this -> api -> user_want_emails || $appCore -> user['id'] == $this -> api -> user_id),
					'user_show_email'		=> ($appCore -> user_acl['email_access'] == 0 ? 0 : $appCore -> user_acl['email_access'] == 2 || $this -> api -> user_show_email || $appCore -> user['id'] == $this -> api -> user_id),
					'user_title'			=> $this -> api -> user_title,
					'user_avatar_type'		=> ($appCore -> user['id'] == $this -> api -> user_id || $appCore -> user_acl['can_see_users_avatars'] ? $this -> api -> user_avatar_type : 0),
					'user_avatar_image'		=> class_users::avatarLocation($this -> api -> user_avatar_type) . htmlspecialchars($this -> api -> user_avatar_image),
					'user_avatar_width'		=> $this -> api -> user_avatar_width,
					'user_avatar_height'	=> $this -> api -> user_avatar_height,
					'user_join_date'		=> $this -> api -> user_join_date,
					'user_last_date'		=> $this -> api -> user_last_date,
					'user_posts'			=> $this -> api -> user_posts,
					'user_last_post'		=> $this -> api -> user_last_post,
					'user_main_group'		=> '',
					'user_other_groups'		=> '',
				);
				
				// Make CORE load User Groups
				$appCore -> users -> getUserGroups( false, false);
												
				// Reiterate em
				foreach ($appCore -> cache_store['user_groups'] as $group_id => $group_data)
				{
					// User group?
					if ( in_array($group_id, $this -> api -> user_groups))
					{
						// Main group?
						if ( $this -> api -> group_id == $group_id)
						{							
							// Draw group name
							$tpl_vars['user_main_group'] .= $group_data['group_prefix'] . $group_data['group_name'] . $group_data['group_suffix'];
							
							// Decide on users rank
							$user_rank = $appCore -> users -> getRank( $this -> api -> user_rank, $group_data['group_rank'], $this -> api -> user_posts);
							
							// Set title?
							if (!isset($tpl_vars['user_title'][0]))
							{
								$tpl_vars['user_title'] = $user_rank[0];
							}
							
							// Set Image
							$tpl_vars['user_rank_image'] = $user_rank[1];
							
							// Garbage out
							unset($user_rank);
						}
						else
						{
							// Draw other group
							$tpl_vars['user_other_groups'] .= '<div>' . $group_data['group_prefix'] . $group_data['group_name'] . $group_data['group_suffix'] . '</div>';
						}
					}
				}
								
				// Set users timezone
				$timezones = $appCore -> getTimeZones();
				
				// Return user's timezone
				if ( isset($timezones[$this -> api -> user_timezone]))
				{
					$tpl_vars['user_timezone'] = $timezones[$this -> api -> user_timezone];
				}

				// Set current time in users place
				if ($this -> api -> user_timezone * 3600 == $this -> appCore -> time_zone && $this -> api -> user_dst == $this -> appCore -> time_dst)
				{
					$tpl_vars['user_time'] = $appCore -> getDate($appCore -> time, 'long', false);
				}
				else
				{
					// Set fallback date config
					$org_time_zone = $this -> appCore -> time_zone;
					$org_time_dst = $this -> appCore -> time_dst;
					
					// Set new time
					$this -> appCore -> time_zone = $this -> api -> user_timezone * 3600;
					$this -> appCore -> time_dst = $this -> api -> user_dst;
					
					// Draw Date
					$tpl_vars['user_time'] = $appCore -> getDate($appCore -> time, 'long', false);
					
					// Reset config
					$this -> appCore -> time_zone = $org_time_zone;
					$this -> appCore -> time_dst = $org_time_dst;
					
					// Garbage out
					unset( $org_time_zone);
					unset( $org_time_dst);
				}
				
				// User signature?
				if ( isset($this -> api -> user_signature_raw[0]) && ($appCore -> user_acl['can_see_users_signatures'] || $appCore -> user['id'] == $this -> api -> user_id))
				{
					// Start Parser
					$parser = new class_parser($appCore, true);
					
					// Parse signature
					$tpl_vars['user_signature'] = $parser -> finalise($this -> api -> user_signature);
					
					// Nail Parser
					unset($parser);
				}
				
				// User online?
				if ( $appCore -> conf['sessions_tracker'])
				{
					// Start Tracker
					$tracker = new class_sessions_tracker($appCore);
					
					// Draw User status
					$tpl_vars['user_status_show'] = 1;
					$tpl_vars['user_status'] = $tracker -> checkStatus($this -> api -> user_id);
					
					// Send tracker back to heavens
					unset($tracker);
				}
				
				// Draw Page
				$appCore -> output -> draw( htmlspecialchars($this -> api -> user_name), array(
					array(
						$appCore -> niceLink('act=users', $appCore -> lang['board_nav_users']),
						$appCore -> lang['board_nav_users']
					),
					array(
						$appCore -> niceLink('act=user&user=' . $this -> api -> user_id, $this -> api -> user_name),
						htmlspecialchars($this -> api -> user_name)
					)
				), $templates -> tpl_profile_body( $tpl_vars));			
			}
		}
		
	}
	
}